package com.dd.cloud.common.annotate.validation.impl;

import com.dd.cloud.common.annotate.validation.Fee;
import com.dd.cloud.common.constants.RegularConstants;
import com.dd.cloud.common.utils.RegexUtils;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.validator.constraintvalidation.HibernateConstraintValidatorContext;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

/**
 * 代煎费、免煎费、快递费、诊疗费、提现金额、药材单价验证实现类
 *
 * @author zk
 */
public class FeeValidation implements ConstraintValidator<Fee, Object> {
    private Fee fee;

    @Override
    public void initialize(Fee feeVal) {
        fee = feeVal;
    }

    @Override
    public boolean isValid(Object value, ConstraintValidatorContext context) {
        Integer ph = (Integer) value;
        if (!fee.required() && ph == null) {
            return true;
        }
        if (fee.required() && ph == null) {
            return false;
        }
        //如果设置的返回的消息参数信息,则替换参数
        if (!StringUtils.isBlank(fee.begin())){
            context.unwrap( HibernateConstraintValidatorContext.class ).addMessageParameter( "begin", fee.begin() );
        }
        return RegexUtils.regCheck(ph, RegularConstants.FEE_REGEXP);
    }
}
